﻿package {
	/*
	** @name			: PicLens.as
	** @description		: Test PicLens in Flash
	** @author			: =undo=
	** @web				: http://www.undolog.com
	** @email			: g.fazioli@undolog.com
	** 
	** @ver				: 1.0
	*/
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	
	/*
	** Import Papervision3D 2.0
	*/
	import org.papervision3d.events.InteractiveScene3DEvent;  
	import org.papervision3d.materials.*;  
	import org.papervision3d.objects.DisplayObject3D;  
	import org.papervision3d.objects.primitives.Plane;  
	import org.papervision3d.view.BasicView;

	/*
	** Import UndoLibrary
	*/
	import undolibrary.ui.*;
	//
	public class PicLens extends Sprite {
		// define constant
		private const THUMBNAIL_WIDTH:uint		= 300;
		private const THUMBNAIL_HEIGHT:uint 	= 300;
		private const THUMBNAIL_MARGIN:uint 	= 50;
		private const THUMBNAIL_NUMBER:uint		= 99;
		private const CAMERA_ZOOM:uint 			= 3;
		// properties
		private var __slider:Knob;
		private var __max:Number;
		private var __imagexml:XML;
		private var __thnumber:Number			= 0;
		private var __imgarray:Array 			= [];
		private var __matarray:Array 			= [];
		// test knob controls
		private var __focusCamera:Knob;
		private var __zoomCamera:Knob;
		private var __fooTargetPosition:Knob;
		
		// PV3D 2.0
		private var __bv:BasicView;
		private var __fooTarget:DisplayObject3D;
		private var __material:BitmapAssetMaterial;
		
		/*
		** Constructor
		*/
		function PicLens() {
			addEventListener(Event.ADDED_TO_STAGE, init );
		}

		/*
		** @name			: init()
		*/
		private function init( e:Event ):void {
			initPapervision();
			initMaterials();
			initObjects();
			initControls();
			initListeners();
		}

		/*
		** @name			: initPapervision()
		** @description		: inizializza l'ambiente papervision
		*/
		private function initPapervision():void {
			__bv 				= new BasicView(640, 480, false, true);
			addChild( __bv );
			__bv.camera.zoom	= 2;
		}
		
		/*
		** @name			: initMaterials()
		** @description		: creo 100 piani con un'immagine - sia da RSS che non
		*/
		private function initMaterials():void {
			__thnumber 	= THUMBNAIL_NUMBER; 
			var k 		= 0;
			var	i;
			//
			for(i=0; i < __thnumber; i++) {
				__matarray.push( new BitmapAssetMaterial("foto1") );
				k++;
			}
		}
		
		/*
		** @name			: initObjects()
		*+ @description		: crea il rettabgolo 3*100
		*/
		private function initObjects():void {
			__fooTarget 		= new DisplayObject3D();
			//
			var sx:Number 		= -THUMBNAIL_WIDTH*4;
			var sy:Number 		= THUMBNAIL_HEIGHT*1.5;
			var sz:Number 		= 0;
			//
			var xx:Number 		= 0
			var yy:Number 		= 0;
			var zz:Number 		= 0;
			//
			__fooTarget.z		= sz + 10;
			//			
			for(var i=0; i < __thnumber; i++) {
				__matarray[i].smooth		 = true;
				__matarray[i].interactive	 = true; 
				var photo1:Plane= new Plane( __matarray[i], THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, 0, 0);
				xx 				= sx + ( (THUMBNAIL_WIDTH+THUMBNAIL_MARGIN) * Math.floor( i/3 ) );
				__bv.scene.addChild(photo1);
				photo1.x 		= xx;
				photo1.y 		= sy + (yy - (THUMBNAIL_HEIGHT+THUMBNAIL_MARGIN)*(i%3));
				photo1.z 		= sz + zz;
				photo1.addEventListener( InteractiveScene3DEvent.OBJECT_CLICK,
					function(e:InteractiveScene3DEvent):void {
						new Tween(e.displayObject3D, 'z', Strong.easeOut, e.displayObject3D.z, 5000, 1, true);
					}
				);
			}
			__max 				= xx;
			__bv.cameraAsCamera3D.target 	= __fooTarget; 
		}

		/*
		** @name			: initControl()
		** @description		: aggiungo un semplice slider per scorrere e ricreare l'effetto PicLens
		*/
		private function initControls():void {
			var panel:Sprite		= new Sprite();
			addChild( panel );
			with( panel.graphics ) {
				beginFill( 0x333333 );
				drawRect( 0, 0, stage.stageWidth, 100 );
				endFill();
			}
			panel.y					= stage.stageHeight - 100;
			// slider principale
			__slider 				= new Knob();
			panel.addChild( __slider );
			__slider.x				= 5;
			__slider.y 				= panel.height - 25;
			__slider.Color			= 0x111111;
			__slider.KnobColor		= 0x0088ff;
			__slider.Width			= stage.stageWidth-10;
			__slider.KnobWidth		= 20;
			__slider.Height			= __slider.KnobHeight = 20;
			__slider.Max 			= __max;
			//
			__focusCamera			= new Knob();
			panel.addChild( __focusCamera );
			__focusCamera.x			= 80;
			__focusCamera.y 		= 5;
			__focusCamera.KnobWidth	= 10;
			__focusCamera.KnobColor	= 0xff9900;
			__focusCamera.Min		= 50;
			__focusCamera.Max		= 400;
			__focusCamera.Value		= __bv.cameraAsCamera3D.focus;
			createLabel(__focusCamera, 'focus');
			//
			__zoomCamera			= new Knob();
			panel.addChild( __zoomCamera );
			__zoomCamera.x			= 80;
			__zoomCamera.y 			= 25;
			__zoomCamera.KnobWidth	= 10;
			__zoomCamera.KnobColor	= 0xff9900;
			__zoomCamera.Min		= 0;
			__zoomCamera.Max		= 10;
			__zoomCamera.Value		= __bv.cameraAsCamera3D.zoom;
			createLabel(__zoomCamera, 'zoom');
			//
			__fooTargetPosition		= new Knob();
			panel.addChild( __fooTargetPosition );
			__fooTargetPosition.x			= 80;
			__fooTargetPosition.y 			= 45;
			__fooTargetPosition.KnobWidth	= 10;
			__fooTargetPosition.KnobColor	= 0xff9900;
			__fooTargetPosition.Min			= -1000;
			__fooTargetPosition.Max			= 1000;
			__fooTargetPosition.Value		= __fooTarget.z;
			createLabel(__fooTargetPosition, 'target');
		}
		
		/*
		** @name			: createLabel()
		*/
		private function createLabel(s:Knob, l:String ):void {
			var lab:LabelText		= new LabelText();
			lab.autoSize 			= true;
			lab.label.text			= l.toUpperCase();
			lab.x					= -60;
			s.addChild( lab );
			//
			var val:LabelText		= new LabelText();
			val.name 				= 'labeliana';
			val.autoSize 			= true;
			val.label.text			= String( Math.round( s.Value ) ).toUpperCase();
			val.x					= 200;
			s.addChild( val );
			//
			s.addEventListener( KnobEvent.CHANGE, 
				function(e:KnobEvent):void {
					e.currentTarget.getChildByName('labeliana').label.text = String( Math.round( e.Value ) );
				}
			);
		}
		
		/*
		** @name			: initListeners()
		** @description		: impostazione degli eventi
		*/
		private function initListeners():void {
			addEventListener( Event.ENTER_FRAME, render );
		}

		/*
		** @prototype
		*/
		private function render(e:Event):void {
			__bv.cameraAsCamera3D.zoom		= __zoomCamera.Value;
			__bv.cameraAsCamera3D.focus		= __focusCamera.Value;
			__bv.cameraAsCamera3D.x 		+= (( __slider.Value - __bv.cameraAsCamera3D.x)/10)/2.2;
			__fooTarget.x 					+= (( __slider.Value - __fooTarget.x)/5)/2.2;
			__fooTarget.z					= __fooTargetPosition.Value;
			__bv.singleRender();  
		}
	}
}